//
//  SearchResultsViewController.m
//  QinrenManagers
//
//  Created by easaa on 15/7/16.
//  Copyright (c) 2015年 easaa. All rights reserved.
//

#import "SearchResultsViewController.h"
#import "DetailSaleViewController.h"

#import "PolicyModel.h"

@interface SearchResultsViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchDisplayDelegate>

@property (nonatomic, strong) UITableView    * salesPolicyTableView;

@property (nonatomic, strong) UISearchBar    * searchBar;

@property (nonatomic, strong) UISearchDisplayController * searchController;

@property (nonatomic, strong) NSMutableArray * resultArray;

@end

@implementation SearchResultsViewController


- (instancetype)init
{
    self = [super init];
    if (self) {
        
        _dataSourceArry = [NSMutableArray array];
        _nameArray = [NSMutableArray array];
        _resultArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self creatUI];
}


- (void)creatUI
{
    
    //搜索框
    _searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, 44)];
    //_searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 64, 200, 44)];
    
    
    _searchBar.placeholder = @"搜索";
    _searchBar.backgroundColor = [UIColor whiteColor];
    
    //搜索结果展示
    // 用 searchbar 初始化 SearchDisplayController
    // 并把 searchDisplayController 和当前 controller 关联起来
    _searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
    
    _searchController.delegate = self;
    // searchResultsDataSource 就是 UITableViewDataSource
    _searchController.searchResultsDataSource = self;
    // searchResultsDelegate 就是 UITableViewDelegate
    _searchController.searchResultsDelegate = self;
    
    
    
    //主界面数据展示
    // _salesPolicyTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_searchBar.frame), SCREEN_W, SCREEN_H-64-44) style:UITableViewStyleGrouped];
    _salesPolicyTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H) style:UITableViewStylePlain];
    
    
    //_salesPolicyTableView.backgroundColor = [UIColor yellowColor];
    _salesPolicyTableView.dataSource=self;
    _salesPolicyTableView.delegate=self;
    
    _salesPolicyTableView.tableHeaderView = _searchBar;
    _salesPolicyTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    
    _salesPolicyTableView.backgroundColor = [UIColor whiteColor];
    
    //[self.view addSubview:_searchBar];
    [self.view addSubview:_salesPolicyTableView];
    
    
}

//只要用户在searchbar中输入内容就会触发该方法
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    //每次搜索之前，清空searchResults
    [_resultArray removeAllObjects];
    NSLog(@"%@", searchString);
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",searchString];//用于过滤
    _resultArray = [NSMutableArray arrayWithArray:[_nameArray filteredArrayUsingPredicate:predicate]];
    
    return YES;
}


#pragma mark- ------------------------UITableView 代理实现--------------------------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    if(tableView != _salesPolicyTableView)
    {
        return 1;
    }
    
    //return _dataSourceArry.count;
    return 1;
    
}

//设置rows的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView != _salesPolicyTableView)
    {
        return [_resultArray count];
    }
    //    else
    //        return [[_dataSourceArry objectAtIndex:section]count];
    else
        //return [_dataSourceArry count];
        return _dataSourceArry.count;
    
    
    
}


//Cell的相关设置
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellName=@"cell";
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if(cell==nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellName];
    }
    if(tableView != _salesPolicyTableView){
        cell.textLabel.text=[_resultArray objectAtIndex:indexPath.row];
    }else{
        
        PolicyModel *policyModel = _dataSourceArry[indexPath.row];
        
        cell.textLabel.text=policyModel.name;
        policyModel.addtime =[policyModel.addtime stringByReplacingOccurrencesOfString:@"+" withString:@" "];
        cell.detailTextLabel.text = policyModel.addtime;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath

{
    DetailSaleViewController *detailSaleViewController = [[DetailSaleViewController alloc] init];
    PolicyModel *policyModel = _dataSourceArry[indexPath.row];
    detailSaleViewController.title = @"政策详情";
    detailSaleViewController.policyTitle = policyModel.name;
    detailSaleViewController.article_id = policyModel.aid;
    [self.navigationController pushViewController:detailSaleViewController animated:NO];
}


//设置section的title
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if(tableView != _salesPolicyTableView){
        return @"政策搜索结果";
    }
    else{
        
        //return [NSString stringWithFormat:@"%c",97+section];
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}




@end
